layui.define(['layer', 'laytpl', 'element'], function(exports) {
  var $ = layui.jquery,
    layer = layui.layer,
    _modName = 'timetick',
    _win = $(window),
    _doc = $(document),
    laytpl = layui.laytpl,
    element = layui.element;

  var timetick = {
    v: '1.0.5',
    config: {
      rate:1000,
      formart:"yyyy-MM-dd hh:mm:ss EEE",
      elem: undefined,
    },
    set: function(options) {
      var that = this; 
      $.extend(true, that.config, options);
      that.tick();
      return that;
    },
    /**
     * 是否已设置了elem
     */
    hasElem: function() {
      var that = this,
        _config = that.config;
      if (_config.elem === undefined && $(_config.elem)) {
        layui.hint().error('timetick error:请配置元素');
        return false;
      }
      return true;
    },
    /**
     * 获取容器的jq对象
     */
    getElem: function() {
      var _config = this.config;
      return  $(_config.elem);
    }, 
    /**
     * 渲染
     */
    render: function(callback) { 
      var that = this;
      var tIndex = setInterval(function() { 
        that.tick();
      }, timetick.config.rate);
      return that;
    },
    tick:function(){
      var t=this.format(new Date(),timetick.config.formart); 
      $(timetick.config.elem).text(t);
    },
    format:function(date,fmt) {         
      var o = {         
      "M+" : date.getMonth()+1, //月份         
      "d+" : date.getDate(), //日         
      "h+" : date.getHours()%12 == 0 ? 12 : date.getHours()%12, //小时         
      "H+" : date.getHours(), //小时         
      "m+" : date.getMinutes(), //分         
      "s+" : date.getSeconds(), //秒         
      "q+" : Math.floor((date.getMonth()+3)/3), //季度         
      "S" : date.getMilliseconds() //毫秒         
      };         
      var week = {         
      "0" : "日",         
      "1" : "一",         
      "2" : "二",         
      "3" : "三",         
      "4" : "四",         
      "5" : "五",         
      "6" : "六"        
      };         
      if(/(y+)/.test(fmt)){         
          fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));         
      }         
      if(/(E+)/.test(fmt)){         
          fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "星期" : "/星期") : "")+week[date.getDay()+""]);         
      }         
      for(var k in o){         
          if(new RegExp("("+ k +")").test(fmt)){         
              fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));         
          }         
      }         
      return fmt;         
  }       
  };
  exports(_modName, timetick);
});